package models

import (
	"fmt"
	"time"
)

type Enterprise struct {
	Id                       int       `json:"id"`
	IsIdent                  int       `json:"is_ident"`
	IdentData                string    `json:"ident_data"`
	Name                     string    `json:"name"`
	Logo                     string    `json:"logo"`
	Industry                 string    `json:"industry"`
	PlacePartnerId           int       `json:"place_partner_id"`
	OperatorId               int       `json:"operator_id"`
	SalesmanId               int       `json:"salesman_id"`
	Maibao                   float64   `json:"maibao"`
	FenxiaoRewardGoldCurrent float64   `json:"fenxiao_reward_gold_current"`
	FenxiaoRewardGoldTotal   float64   `json:"fenxiao_reward_gold_total"`
	Invitation               string    `json:"invitation"`
	Introduce                string    `json:"introduce"`
	WebSite                  string    `json:"web_site"`
	ProvinceId               int       `json:"province_id"`
	CityId                   int       `json:"city_id"`
	AreaId                   int       `json:"area_id"`
	AreaName                 string    `json:"area_name"`
	Address                  string    `json:"address"`
	CreatedAt                time.Time `json:"created_at"`
	UpdatedAt                time.Time `json:"updated_at"`
	VipFeeConfigId           int       `json:"vip_fee_config_id"`
	VipFeeConfigComment      string    `json:"vip_fee_config_comment"`
	OveredAt                 time.Time `json:"overed_at"`
	Status                   int       `json:"status"`
}

func (d *Enterprise) TableName() string {
	return "enterprise"
}

func GetEnterpriseList(page int, limit int, identStatus, status, staffName, staffPhone, comName, beginTime string) (interface{}, *Paginator) {
	var res []*struct {
		Id                     int     `json:"id"`
		Name                   string  `json:"name"`
		ComName                string  `json:"com_name"`
		StaffNum               int     `json:"staff_num"`
		VipNum                 int     `json:"vip_num"`
		MemberNum              int     `json:"member_num"`
		FenxiaoRewardGoldTotal float64 `json:"fenxiao_reward_gold_total"`
		Status                 int     `json:"status"`
		IsIdent                int     `json:"is_ident"`
		Account                string  `json:"account"`
		Uid                    int     `json:"uid"`
	}
	m := DB.Table("enterprise e").Select("e.id,e.name as com_name,e.is_ident,eu.status,e.fenxiao_reward_gold_current,es.name,eu.account,eu.id as uid").
		Joins("left join enterprise_staff es on e.id = es.enterprise_id ").
		Joins("left join enterprise_users eu on e.id = eu.enterprise_id").
		Order("e.id DESC").Order("e.is_ident DESC")
	if status != "" {
		m = m.Where("eu.status=?", status)
	}
	if identStatus != "" {
		m = m.Where("e.is_ident=?", identStatus)
	}
	if staffName != "" {
		m = m.Where("es.name=?", staffName)
	}
	if staffPhone != "" {
		m = m.Where("eu.account=?", staffPhone)
	}
	if comName != "" {
		m = m.Where("e.name=?", comName)
	}
	if beginTime != "" {
		m = m.Where("e.created_at > ?", beginTime)
	}
	m = m.Where("es.user_id = ?", 0)
	var count int
	m.Count(&count)
	paginate := NewPage(page, limit, count)

	err := m.Offset(paginate.Offset).Limit(paginate.Limit).Scan(&res).Error
	if err != nil {
		return nil, nil
	}

	for _, item := range res {
		staffNum := 0
		m := DB.Table("enterprise_staff").Where("enterprise_id = ?", item.Id)
		m.Count(&staffNum)
		item.StaffNum = staffNum
		item.MemberNum = staffNum - 1
		var staff []*struct {
			UserId int
		}
		m.Where("user_id != ?", 0).Scan(&staff)
		vipNum := staffNum - 1
		for _, s := range staff {
			var grade struct {
				OveredAt time.Time
			}
			DB.Table("user_grade").Select("user_id,overed_at").Where("user_id = ?", s.UserId).Find(&grade)
			if grade.OveredAt.Before(time.Now()) {
				vipNum--
			}
		}

		item.VipNum = vipNum
	}

	return res, &paginate
}

func GetIdentList(page int, limit int, identStatus, status, staffName, staffPhone, comName, beginTime string) (interface{}, *Paginator) {
	var res []*struct {
		Id        int       `json:"id"`
		Name      string    `json:"name"`
		ComName   string    `json:"com_name"`
		Status    int       `json:"status"`
		IsIdent   int       `json:"is_ident"`
		Account   string    `json:"account"`
		Job       string    `json:"job"`
		IdentTime time.Time `json:"ident_time"`
		Uid       int       `json:"uid"`
	}
	m := DB.Table("enterprise e").Select("e.id,e.name as com_name,e.is_ident,e.ident_time,eu.status,es.name,es.job,eu.account,eu.id as uid").
		Joins("left join enterprise_staff es on e.id = es.enterprise_id ").
		Joins("left join enterprise_users eu on e.id = eu.enterprise_id").
		Order("e.id DESC").Order("e.is_ident DESC")
	if status != "" {
		m = m.Where("eu.status=?", status)
	}
	if identStatus != "" {
		m = m.Where("e.is_ident=?", identStatus)
	}
	if staffName != "" {
		m = m.Where("es.name=?", staffName)
	}
	if staffPhone != "" {
		m = m.Where("eu.account=?", staffPhone)
	}
	if comName != "" {
		m = m.Where("e.name=?", comName)
	}
	if beginTime != "" {
		m = m.Where("e.created_at > ?", beginTime)
	}
	m = m.Where("es.user_id = ?", 0)
	var count int
	m.Count(&count)
	paginate := NewPage(page, limit, count)

	err := m.Offset(paginate.Offset).Limit(paginate.Limit).Scan(&res).Error
	if err != nil {
		return nil, nil
	}

	return res, &paginate
}

// 城市合伙人
func (e *Enterprise) GetPlacePartnerIdList(page, limit int) ([]*Enterprise, *Paginator) {

	var count int
	DB.Model(e).Where("place_partner_id=?", e.PlacePartnerId).Count(&count)
	paginate := NewPage(page, limit, count)

	var enterprise []*Enterprise
	err := DB.Model(e).Where("place_partner_id=?", e.PlacePartnerId).Offset(paginate.Offset).Limit(paginate.Limit).Find(&enterprise).Error
	if err != nil {
		return nil, nil
	}
	return enterprise, &paginate
}

// 运营
func (e *Enterprise) GetOperatorIdList(page, limit int) ([]*Enterprise, *Paginator) {

	var count int
	DB.Model(e).Where("operator_id=?", e.OperatorId).Count(&count)
	paginate := NewPage(page, limit, count)

	var enterprise []*Enterprise
	err := DB.Model(e).Where("operator_id=?", e.OperatorId).Offset(paginate.Offset).Limit(paginate.Limit).Find(&enterprise).Error
	if err != nil {
		return nil, nil
	}
	return enterprise, &paginate
}

// 业务员
func (e *Enterprise) GetSalesmanIdList(page, limit int) ([]*Enterprise, *Paginator) {

	var count int
	DB.Model(e).Where("salesman_id=?", e.SalesmanId).Count(&count)
	paginate := NewPage(page, limit, count)

	var enterprise []*Enterprise
	err := DB.Model(e).Where("salesman_id=?", e.SalesmanId).Offset(paginate.Offset).Limit(paginate.Limit).Find(&enterprise).Error
	if err != nil {
		return nil, nil
	}
	return enterprise, &paginate
}
func EnterpriseRechargesList(page, limit int, comName, phone, beginTime, status string) (interface{}, *Paginator) {
	var res []*struct {
		Id                  int       `json:"id"`
		Name                string    `json:"name"`
		ComName             string    `json:"com_name"`
		Status              int       `json:"status"`
		Account             string    `json:"account"`
		VipFeeConfigComment string    `json:"vip_fee_config_comment"`
		Money               string    `json:"money"`
		OveredAt            time.Time `json:"overed_at"`
		PayTime             time.Time `json:"pay_time"`
		PayType             string    `json:"pay_type"`
		EnterpriseId        int       `json:"enterprise_id"`
	}
	m := DB.Table("enterprise_recharges er").Select("er.id,er.enterprise_id,e.name as com_name,er.pay_time,er.pay_type,er.vip_fee_config_comment,er.overed_at,er.money,eu.status,es.name,eu.account").
		Joins("left join enterprise e on e.id = er.enterprise_id").
		Joins("left join enterprise_staff es on er.staff_id = es.id").
		Joins("left join enterprise_users eu on er.enterprise_id = eu.enterprise_id").
		Order("er.id DESC").Where("er.is_pay = ?", 1)
	if status != "" {
		m = m.Where("eu.status=?", status)
	}
	if phone != "" {
		m = m.Where("eu.account=?", phone)
	}
	if comName != "" {
		m = m.Where("e.name like ? ", fmt.Sprintf("%%%v%%", comName))
	}
	if beginTime != "" {
		m = m.Where("er.created_at > ?", beginTime)
	}

	var count int
	m.Count(&count)
	paginate := NewPage(page, limit, count)

	err := m.Offset(paginate.Offset).Limit(paginate.Limit).Scan(&res).Error
	if err != nil {
		return nil, nil
	}

	return res, &paginate
}
func EnterResers(page, limit int, comName, phone, status string) (interface{}, *Paginator) {
	var res []*struct {
		Id                       int     `json:"id"`
		Name                     string  `json:"name"`
		ComName                  string  `json:"com_name"`
		Status                   int     `json:"status"`
		Account                  string  `json:"account"`
		FenxiaoRewardGoldCurrent float64 `json:"fenxiao_reward_gold_current"`
		FenxiaoRewardGoldTotal   float64 `json:"fenxiao_reward_gold_total"`
	}
	m := DB.Table("enterprise e").Select("e.id,e.name as com_name,e.fenxiao_reward_gold_current,e.fenxiao_reward_gold_total,es.name,eu.account,eu.status").
		Joins("left join enterprise_users eu on e.id = eu.enterprise_id").
		Joins("left join enterprise_staff es on eu.staff_id = es.id").
		Order("e.id DESC")
	if status != "" {
		m = m.Where("eu.status=?", status)
	}
	if phone != "" {
		m = m.Where("eu.account=?", phone)
	}
	if comName != "" {
		m = m.Where("e.name like ? ", fmt.Sprintf("%%%v%%", comName))
	}

	var count int
	m.Count(&count)
	paginate := NewPage(page, limit, count)

	err := m.Offset(paginate.Offset).Limit(paginate.Limit).Scan(&res).Error
	if err != nil {
		return nil, nil
	}

	return res, &paginate
}
